package vsm.vsm_server.component.oss;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;
import lombok.extern.slf4j.Slf4j;
import vsm.vsm_server.pojo.Image.ImageInfo;
import vsm.vsm_server.pojo.Video.VideoCreateDTO;


@Slf4j
public class InitAcsClint {



    /**
     * 读取AccessKey信息
     */
    public static DefaultAcsClient initVodClient (String accessKeyId, String accessKeySecret) throws ClientException {
        String regionId = "cn-shanghai";  // 点播服务接入区域,固定的
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    /**
     * 刷新音/视频上传凭证
     * @param client 发送请求客户端
     * @return RefreshUploadVideoResponse 刷新音/视频上传凭证响应数据
     * @throws Exception
     */
    public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient client,String videoId) throws Exception {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        //音频或视频ID
        request.setVideoId(videoId);
        return client.getAcsResponse(request);
    }
    /**
     * 获取音/视频上传地址和凭证
     * @param client 发送请求客户端
     * @return CreateUploadVideoResponse 获取音/视频上传地址和凭证响应数据
     * @throws Exception
     */
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient client, VideoCreateDTO videoCreateDTO) throws Exception {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        log.info(String.valueOf(videoCreateDTO));
        request.setTitle(videoCreateDTO.getTitle());
        request.setFileName("this is a sample.mp4");
        request.setDescription(videoCreateDTO.getDescription());
        //UserData，用户自定义设置参数，用户需要单独回调URL及数据透传时设置(非必须)
        JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置，指定时以此为准，否则以全局设置的事件通知为准
        JSONObject messageCallback = new JSONObject();


        //设置回调地址
        messageCallback.put("CallbackURL", "http://4wp1118002ub.vicp.fun/callback/videoup");
        //设置回调方式，默认为http
        messageCallback.put("CallbackType", "http");
        userData.put("MessageCallback", messageCallback.toJSONString());

        //UserData透传数据部分设置
        //用户自定义的扩展字段，用于回调时透传返回
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());

        request.setUserData(userData.toJSONString());

        return client.getAcsResponse(request);
    }

    public static CreateUploadImageResponse createUploadImage(DefaultAcsClient client, ImageInfo imageInfo) throws  Exception{
        CreateUploadImageRequest request = new CreateUploadImageRequest();
        request.setTitle(imageInfo.getTitle());
        request.setImageType(imageInfo.getType());



        //UserData，用户自定义设置参数，用户需要单独回调URL及数据透传时设置(非必须)
        JSONObject userData = new JSONObject();

        //UserData回调部分设置
        //消息回调设置，指定时以此为准，否则以全局设置的事件通知为准
        JSONObject messageCallback = new JSONObject();


        //设置回调地址
        messageCallback.put("CallbackURL", "http://4wp1118002ub.vicp.fun/callback/imageup");
        //设置回调方式，默认为http
        messageCallback.put("CallbackType", "http");
        userData.put("MessageCallback", messageCallback.toJSONString());

        //UserData透传数据部分设置
        //用户自定义的扩展字段，用于回调时透传返回
        //JSONObject extend = new JSONObject();
        //extend.put("MyId", "user-defined-id");
        //userData.put("Extend", extend.toJSONString());

        request.setUserData(userData.toJSONString());

        return client.getAcsResponse(request);
    }
}
